package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetRankingListBean;
import com.rjwh.dingdong.client.bean.localbean.BannerData;
import com.rjwh.dingdong.client.bean.localbean.GetRankingListBean;
import com.rjwh.dingdong.client.bean.localbean.ScoreRankListItemData;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.HomeWorkRankModel;
import com.rjwh.dingdong.client.mvp.presenter.IHomeWorkRankPresenter;
import com.rjwh.dingdong.client.mvp.view.HomeWorkRankView;
import com.rjwh.dingdong.client.util.ToastUtil;

import java.util.List;

/**
 * @author 王小珂
 * @ClassName HomeWorkRankPresenter
 * @Description
 * @date 2017-1-5
 * @history 2017-1-5 author: description:
 */

public class HomeWorkRankPresenter extends BasePresenter<HomeWorkRankView> implements IHomeWorkRankPresenter {

    private HomeWorkRankModel model;
    private SwipyRefreshLayoutDirection mDirection;

    public HomeWorkRankPresenter(Context context, HomeWorkRankView mMvpView) {
        super(context, mMvpView);
        model = new HomeWorkRankModel(this);
    }

    /**
     * 作业分享积分排行榜
     *
     * @param pageindex
     * @param isRefresh
     */
    @Override
    public void doGetHomeWorkList(String duration,int pageindex, boolean isRefresh,SwipyRefreshLayoutDirection direction) {
        mDirection = direction;
        model.doGetHomeWorkList(duration,pageindex, isRefresh);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (getMvpView() != null) {
            switch (businessType) {
                case NetConstant.GETRANKINGLIST:
                    getMvpView().setRefreshing(false);
                    ResGetRankingListBean resGetRankingListBean = (ResGetRankingListBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGetRankingListBean != null) {
                        GetRankingListBean data = resGetRankingListBean.getData();
                        if (data != null) {
                            List<ScoreRankListItemData> homeworkscoreranklist = data.getHomeworkscoreranklist();
                            String ruleactionurl = data.getRuleactionurl();
                            String ranktitle = data.getRanktitle();
                            List<BannerData> bannerlist = data.getBannerlist();

                            if (homeworkscoreranklist != null) {
                                if (homeworkscoreranklist.isEmpty()) {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                                    if (mDirection == SwipyRefreshLayoutDirection.BOTTOM) {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data_more));
                                    } else {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data));
                                    }
                                } else {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.BOTH);
                                }
                                getMvpView().loadData(isRefresh, homeworkscoreranklist);
                            }
                            if(bannerlist!=null){
                                getMvpView().setBannerData(bannerlist);
                            }
                            getMvpView().setRuleData(ruleactionurl,ranktitle);
                        }
                    } else {
                        getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                        ToastUtil.showToast(getContext(), getContext().getString(R.string.refresh_error));
                    }

                    break;

                default:
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }
}
